package at.technikumwien.trackmydevice.at.technikumwien.trackmydevice;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import at.technikumwien.trackmydevice.R;

public class mapview extends MapActivity 
{
	List<Overlay> mapOverlays;
	DataBaseHandler myDBHAndler = null;
	Cursor query_history_result = null;
	Drawable drawable;
	ItemizedOverlay itemizedOverlay;
	
	/** Called when the activity is first created. */
    @Override
    protected boolean isRouteDisplayed()
    {
    	return false;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapview);
        //Close Button
        Button btn_closeMap = (Button) findViewById(R.id.closeMap);
        btn_closeMap.setOnClickListener(new View.OnClickListener() 
	    {
	    	public void onClick(View v) {
				// TODO Auto-generated method stub
	    		setContentView(R.layout.main_layout);
			}
		});
        //Map settings
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.bulletpoint);
        itemizedOverlay = new ItemizedOverlay(drawable);
        GeoPoint point = new GeoPoint(19240000,-99120000);
    	OverlayItem overlayitem = new OverlayItem(point, "", "");
    	itemizedOverlay.addOverlay(overlayitem);
    	mapOverlays.add(itemizedOverlay);
    	//Datenbank
    	myDBHAndler = new DataBaseHandler(this);
    }
 
    public boolean query_locDB ()
    {
    	boolean success = false;
    	try
    	{
    		SQLiteDatabase queryDB = myDBHAndler.getReadableDatabase();
    		if (queryDB.isOpen())
    		{
    			String[] selected_columns = {"id", "log_name"};
    			query_history_result = queryDB.query(DataBaseHandler.HISTORY_TABLE_NAME, selected_columns, null, null, null, null, "id");
    			query_history_result.close();
    			//queryDB.close();
    			success = true;
    		}
    	}
    	catch (SQLException myExcept) {success = false;}
    	return success;
    }
        
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
    	boolean query_status = query_locDB();
    	if (query_status)
    	{
    		query_history_result.moveToFirst();
    		while (query_history_result.isAfterLast() == false)
    		{
    			menu.addSubMenu(R.id.grp_showTrack, 0, 0, query_history_result.getString(1));
    			query_history_result.moveToNext();
    		}
    	}
    	//Show the menu
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.map_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.track:
        	//Choosing the track to display
        	
            return true;
        case R.id.closeMap:
        	setContentView(R.layout.main_layout);
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
}